﻿// The Nova Project by Ken Beckett.
// Copyright (C) 2007-2012 Inevitable Software, all rights reserved.
// Released under the Common Development and Distribution License, CDDL-1.0: http://opensource.org/licenses/cddl1.php

using System.Collections.Generic;

using Nova.CodeDOM;

namespace Nova.UI
{
    /// <summary>
    /// The view model for a <see cref="CodeDOM.MultiLocalDecl"/>.
    /// </summary>
    public class MultiLocalDeclVM : LocalDeclVM
    {
        #region /* STATICS */

        internal static new void AddViewModelMapping()
        {
            CreateViewModel.Add(typeof(MultiLocalDecl),
                delegate(CodeObject codeObject, bool isDescription, Dictionary<CodeObject, CodeObjectVM> dictionary) { return new MultiLocalDeclVM((MultiLocalDecl)codeObject, dictionary); });
        }

        #endregion

        #region /* FIELDS */

        protected ChildListVM<LocalDeclVM> _localDeclVMs;

        #endregion

        #region /* CONSTRUCTORS */

        /// <summary>
        /// Create a view model instance for the specified <see cref="CodeDOM.MultiLocalDecl"/>.
        /// </summary>
        public MultiLocalDeclVM(MultiLocalDecl multiLocalDecl, Dictionary<CodeObject, CodeObjectVM> dictionary)
            : base(multiLocalDecl, dictionary)
        {
            _localDeclVMs = CreateListVM<LocalDecl, LocalDeclVM>(multiLocalDecl.LocalDecls, dictionary);
        }

        #endregion

        #region /* PROPERTIES */

        /// <summary>
        /// The underlying <see cref="CodeDOM.MultiLocalDecl"/> model.
        /// </summary>
        public MultiLocalDecl MultiLocalDecl
        {
            get { return (MultiLocalDecl)CodeObject; }
        }

        /// <summary>
        /// The list of <see cref="LocalDeclVM"/>s.
        /// </summary>
        public ChildListVM<LocalDeclVM> LocalDecls
        {
            get { return _localDeclVMs; }
        }

        /// <summary>
        /// The number of <see cref="LocalDeclVM"/>s.
        /// </summary>
        public int Count
        {
            get { return _localDeclVMs.Count; }
        }

        /// <summary>
        /// Get the <see cref="LocalDeclVM"/> at the specified index.
        /// </summary>
        public LocalDeclVM this[int index]
        {
            get { return _localDeclVMs[index]; }
        }

        #endregion

        #region /* METHODS */

        #endregion

        #region /* RENDERING */

        protected override void RenderPrefix(CodeRenderer renderer, RenderFlags flags)
        {
            renderer.RenderText(ModifiersHelpers.AsString(MultiLocalDecl.Modifiers), KEYWORD_BRUSH, this);
        }

        protected override void RenderStatement(CodeRenderer renderer, RenderFlags flags)
        {
            RenderFlags passFlags = (flags & RenderFlags.PassMask);
            RenderType(renderer, flags);
            if (MultiLocalDecl.Type == null)
                passFlags |= RenderFlags.NoBorder;
            renderer.RenderList(_localDeclVMs, passFlags | RenderFlags.HasTerminator, this);
        }

        protected override void RenderSuffix(CodeRenderer renderer, RenderFlags flags)
        {
            // The terminator will be rendered by ChildList above, so don't do it here
        }

        public override void RenderVisible(CodeRenderer renderer, RenderFlags flags)
        {
            base.RenderVisible(renderer, flags);
            renderer.RenderVisibleList(_localDeclVMs, flags);
        }

        public override void UnRender()
        {
            ChildListHelpers.UnRender(_localDeclVMs);
            base.UnRender();
        }

        #endregion
    }
}
